import json
import time
import urllib.request
import numpy as np

StatusURL = "http://10.4.0.60:8091/redis/queryVehicleInfoByTerminalCode"
TrackURL = "http://10.4.0.60:8091/redis/queryVehicleTrackInfoByTerminalCode"

StatusBody = {
    "terminalCode": "LFPHC7PE1K1A06216,LFPHC7PE6K1A13713,LFPHC7PE3K1A06587,LFPHC7PE8K1A05600,LFPHC7PE0K1A06501,LFPHC7PE1K1A00982"}
TrackBody = {"terminalCode": "LFPHC7PE3K1A06587", "startTime": "2019-01-29 12:34:56", "endTime": "2019-03-29 23:59:59"}

TestTimes = 100
TestTimeList = []

for index in range(TestTimes):

    TestStartTime = time.time()
    #print("TestStartTime:"+str(TestStartTime))

    params = json.dumps(StatusBody).encode('utf8')

    req = urllib.request.Request(StatusURL, data=params,headers={'content-type': 'application/json'})
    response = urllib.request.urlopen(req)
    ResultResponse = response.read().decode('utf8')
    if(len(ResultResponse)>0):
        #print("第" +str(index+1)+"次，response is OK")
        TestEndTime = time.time()
        # print("TestEndTime:"+str(TestEndTime))
        CastTime = TestEndTime - TestStartTime
        print("第" + str(index + 1) + "次response is OK，耗时" + str(CastTime) + "秒")
        TestTimeList.append(CastTime)
    else:
        print("第" +str(index+1)+"次，response is error")
        break

    time.sleep(0.5)

AverageCostTime = np.mean(TestTimeList)
print("共"+str(TestTimes)+"次"+"平均耗时:"+str(AverageCostTime) + "秒" )